/**
 * Created by WT-SUN on 2017/4/10.
 */
$(function () {
    $('#facilityTb').datagrid({
        rownumbers: true,//显示行号
        striped: true,
        fit: true,
        singleSelect: true,
        sortOrder: true,
        fitColumns: true,
        pagination: true,
        toolbar: '#toolbar',
        url: ROOT_PATH + '/occupationalHealth/facility/facilitiesTable.shtml',
        queryParams:{
            sort:'addTime',
            order:'desc'
        },
        columns: [[
            {
                field: 'id',
                checkbox: true
            },
            {field: 'facilitieName', title: '防护设施名称', width: 100, sortable: true,formatter: function (value, row, index) {
                return "<a href='javascript:void(0)' class='easyui-linkbutton text-primary' plain='true'   onclick='infoFacData(\""+row.id+"\");'>"+row.facilitieName+"</a>";row.id+')">'
            }},
            {field: 'facilitieNum', title: '数量', width: 100},
            {field: 'workPlace', title: '工作场所', width: 100},
            {field: 'facilitieTypeName', title: '防护设施类型', width: 100, sortable: true},
            {field: 'hazardFactorName', title: '危害因素', width: 100},
            {field: 'state', title: '状态', width: 100,formatter: function (value, row, index) {
                return row.state == '0' ? '<span class="color-success text-white" style="padding: 3px;border-radius:5px;">启用</span>' : '<span class="color-important text-white" style="padding: 3px;border-radius:5px;">停用</span>';
            }},
            {field: 'userName', title: '登记人', width: 100},
            {field: 'deptName', title: '登记部门', width: 100},
            {field: 'addTime', title: '登记时间', width: 100}
        ]]
    });
    $('#btn_facility_add').on('click',function () {
        addFacility();
    });
    $('#btn_facility_delete').on('click',function () {
        deleteFacility();
    });
    $('#btn_facility_edit').on('click',function () {
        editFacility();
    });
    var stateData=[{id:'',text:'请选择'},{id:'0',text:'启用'},{id:'1',text:'停用'}];
    pageComData('stateQuery',stateData);
    /**
     * 高级查询
     */
    $('#btn_advanced_search').on("click", function () {
        $('#queryForm2').toggle();
        $('#facilityTb').datagrid('resize');
    });
    $('#btn_clear').on("click", function () {
        $('#queryForm1').form('reset');
        $('#queryForm2').form('reset');
    });
    var code=CONSTANT.DictionaryTypeDefConstants.OHE_FACILITY_TYPE;
    queryComboData(code,'#facilitieTypeQuery');
    queryDepartmentTree();
    /**
     * 绑定人员选择事件
     */
    $("#searchAddUser").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                add_userId_func();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');

            }
        }]
    });
});

var add_userId_func = function () {
    WindowManager.openLayer({
        type: 2,
        title: '选择人员信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
        callBack:function(data){
            getSelectedUser(data);
        }
    });
};
/**
 * 人员信息选择
 */
var getSelectedUser = function (data) {
    $("#searchAddUser").textbox('setValue', data[0].id).textbox('setText', data[0].realName);
};
function queryDepartmentTree(){
    $.ajax({
        url:ROOT_PATH + '/permission/department/getDepartmentTree.shtml',//提交地址
        data:{parentId : 0},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            deptCom('searchAddDept',result);
        }
    });
}
function deptCom(id,result) {
    var deptId='#'+id;
    $(deptId).combotree(
        {
            data:result,
            require:true,
            prompt:'请选择',
            loadFilter:function(data){
                var newData = new Array();
                newData.push({id:"",text:'请选择'});
                newData.push(data[0]);
                return newData;
            }
        }
    );
}

function pageComData(id,data) {
    var pageId='#'+id;
    $(pageId).combobox({
        data: data,
        valueField: 'id',
        textField: 'text',
        editable: false,
        panelHeight: '200',
        selectOnNavigation:true,
    });
}
function queryComboData(code,id) {
    
    var EMPTY_OPTION = {
        id: '',
        text: '请选择'
    };
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            $(id).combobox({
                loadFilter: function (data) {
                    data.unshift(EMPTY_OPTION);
                    return data;
                },
                data: result,
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
                onSelect: function (record) {
                }
            });
        }
    });
}
/**
 * 重新加载表格
 */
function reLoadData() {
    $('#facilityTb').datagrid('clearSelections');
    var queryParams1 = $('#queryForm1').serializeJson();
    var queryParams2 = $('#queryForm2').serializeJson();
    var queryParams = $.extend(queryParams1,queryParams2);
    queryParams["sort"] = 'addTime';
    queryParams["order"] = 'desc';
    $('#facilityTb').datagrid('options').queryParams = queryParams;
    $("#facilityTb").datagrid('reload', ROOT_PATH + '/occupationalHealth/facility/facilitiesTable.shtml');
}
/**
 * 打开弹框
 * @param title
 * @param content
 */
function openLayers(title, content) {
    parent.layer.open({
        type: 2,
        resize: false,
        title: title,
        area: ['1150px', '80%'],
        content: content
    });
}
/**
 * 打开新建
 */
function addFacility() {
    var title = '新增防护设施';
    var content = ROOT_PATH + '/occupationalHealth/facility/add.shtml?resourceId=' + $.getQueryParam('resourceId');
    openLayers(title, content);
}
/**
 * 删除
 */
function deleteFacility() {
    var rows = $('#facilityTb').datagrid('getSelections');

    if (rows.length == 0) {
        layer.alert("选择需要删除的行！");
        return
    }
    var id = rows[0].id;
    layer.confirm('是否确定删除当前选中数据？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.ajax({
            type: 'post',
            url: ROOT_PATH + '/occupationalHealth/facility/deleteById.shtml',
            data: {id: id},
            dataType: "json",
            success: function (result) {
                //result = $.parseJSON(result);
                if (result.success) {
                    $('#facilityTb').datagrid('clearSelections');
                    reLoadData();
                    layer.msg('删除成功', {
                        icon: 1,
                        time: 1000
                    });
                }
                else {
                    layer.msg('删除失败', {
                        icon: 2,
                        time: 1000
                    });
                }
            }
        });
    });
}
/**
 * 编辑
 */
function editFacility() {
    var rows = $('#facilityTb').datagrid('getSelections');
    if (rows.length == 0) {
        layer.alert("选择需要编辑的行！");
        return
    }
    var id = rows[0].id;
    var title = '编辑防护设施';
    var content = ROOT_PATH + '/occupationalHealth/facility/info.shtml?resourceId=' + $.getQueryParam('resourceId')+'&id=' + id+'&type=edit';
    openLayers(title, content);
}
function infoFacData(id) {
    var title = '防护设施详情';
    var content = ROOT_PATH + '/occupationalHealth/facility/info.shtml?resourceId=' + $.getQueryParam('resourceId')+'&id=' + id+'&type=info';
    openLayers(title, content);
}
